package uit.edu.vn.Activity;


import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import uit.edu.vn.Utility.MyActivity;

public class MainActivity extends TabActivity {
    private String userid = "";    
    private String username = "";
    private String password = "";
    public static String address = "";
    private  Intent i;
    private TabHost.TabSpec spec;
    private static final String STRING_NAME_USERSETTING = "USER REFERENCES";
    private SharedPreferences ShareRef = null;
	
	private final int menu_Logout=0;
	/** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        username = getSetting("username");
        password = getSetting("password");
        userid   = getSetting("userid");
        address = getSetting("address");
	      //kiem tra neu username va password rong thi ket thuc activity hien tai va goi activity login
	      if(username == "" && password == ""){
	          Intent intent = new Intent(MainActivity.this, LoginActivity.class);
	          startActivity(intent);
	          finish();            	
	      } 
        setContentView(R.layout.main);
        
        System.out.println("MainActivity");
        System.out.println("username: "+ username);
        System.out.println("password: "+ password);
        System.out.println("userid:   "+ userid);
        System.out.println("address:   "+ getSetting("address"));
      
        //xu ly tab host
        // lay drawable
        final Resources res = getResources(); 
      	final  TabHost tabHost= getTabHost();
  //   TabHost.TabSpec spec;
     final TabHost.TabSpec spec1 = tabHost.newTabSpec("THONGTIN");
     
        // Tạo intent và gọi các class tiep theo
         // Intent i;
        
        //goi BackupActivity.class
        i= new Intent().setClass(this, BackupActivity.class);
        spec= tabHost.newTabSpec("").setIndicator("Sao Lưu",res.getDrawable(R.drawable.tab_backup_selector)).setContent(i);
        
        tabHost.addTab(spec);
        // goi RestoreActivity.class
      i= new Intent().setClass(this, RestoreActivity.class);
        spec= tabHost.newTabSpec("PHUCHOI").setIndicator("Phục Hồi",res.getDrawable(R.drawable.tab_restore_selector)).setContent(i);
        tabHost.addTab(spec);
    
        //goi InfoActivity.class
    i= new Intent().setClass(this, InfoActivity.class);
        spec= tabHost.newTabSpec("THONGTIN").setIndicator("Thông Tin",res.getDrawable(R.drawable.tab_infor_selector)).setContent(i);
        tabHost.addTab(spec);
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if(tabId.equals("THONGTIN")){
					System.out.println(tabId);
					
					System.out.println("tabchange");
					Intent ii= new Intent().setClass(getApplicationContext(), InfoActivity.class);
						
				        spec=tabHost.newTabSpec("THONGTIN").setIndicator("Thông Tin",res.getDrawable(R.drawable.tab_infor_selector)).setContent(ii);
				      tabHost.setCurrentTab(3);
				      System.out.println("changethan cong");
						    
				}
			}
		});
        tabHost.setCurrentTab(0);        
    }
    
    //xu lý menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	// TODO Auto-generated method stub
    	//return super.onCreateOptionsMenu(menu);
    	 menu.add(0, menu_Logout, 0,"Đăng Xuất").setIcon(R.drawable.ic_logout);
		return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
        logOut();
    	finish();
    	return true;
    }
    
	public String getSetting(String key) {
        ShareRef = getSharedPreferences(STRING_NAME_USERSETTING, Context.MODE_WORLD_WRITEABLE);
        return ShareRef.getString(key, "");
	}
    public void saveSetting(String key, String value) {
        ShareRef = getSharedPreferences(STRING_NAME_USERSETTING, Context.MODE_WORLD_WRITEABLE);
        Editor editor = ShareRef.edit();
        editor.putString(key, value);
        editor.commit();
    }
    
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	// TODO Auto-generated method stub
    	if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
    	{
    		showConfirmDialog(getResources().getString(R.string.dialog_thongbao),getResources().getString(R.string.dialog_message_thoat) , new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					logOut();
					finish();
				}
			});
    	}
    	return super.onKeyDown(keyCode, event);
    }
    public AlertDialog showConfirmDialog(String title, String msg, OnClickListener yesClick) 
	{
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setIcon(R.drawable.ic_error);
			if(title!=null) builder.setTitle(title);
			if(msg!=null) builder.setMessage(msg);
			return builder.setPositiveButton(getResources().getString(R.string.txt_Yes), yesClick).setNegativeButton(getResources().getString(R.string.txt_No), new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).show();
	}
    
    public void logOut() {
        saveSetting("username", "");
        saveSetting("password", "");
        saveSetting("userid", "");
    }
}